package com.kitchen.ui.dialog
{
	import com.kitchen.ui.button.CloseButton;
	import com.kitchen.ui.dialog.com.SureButton;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class AbstractDialog extends Sprite
	{
		public static const DIALOG_CLOSE:String = 'dclose';
		public static const DIALOG_SURE:String = 'dsure';
		protected var mainSkin:BitmapData;
		protected var dialogSkin:BitmapData;
		protected var shoper:Object;
		protected var shop:Object;
		public var w:Number;
		public var h:Number;
		protected var timer:Timer;
		protected var state:int;
		protected var toY:int;
		
		public function AbstractDialog(main:BitmapData, dialog:BitmapData, shoper:Object, shop:Object)
		{
			super();
			this.mainSkin = main;
			this.dialogSkin = dialog;
			this.shoper = shoper;
			this.shop = shop;
			this.state = 1;
			this.timer = new Timer(50);
			this.timer.addEventListener(TimerEvent.TIMER, notifyTimer);
		}
		
		protected function addCloseButton():void{
			var closeButton:CloseButton = new CloseButton(this.mainSkin);
			closeButton.name = 'closebtn';
			closeButton.addEventListener(MouseEvent.CLICK, mclickHandle);
			closeButton.x = this.w - 60;
			closeButton.y = 30;
			this.addChild(closeButton);
		}
		
		protected function addSureButton():void{
			var sureButton:SureButton = new SureButton(this.dialogSkin);
			sureButton.name = 'surebtn';
			sureButton.addEventListener(MouseEvent.CLICK, mclickHandle);
			sureButton.x = this.w - sureButton.w-25;
			sureButton.y = this.h - sureButton.h-20;
			this.addChild(sureButton);
		}
		
		protected function mclickHandle(me:MouseEvent):void{
			var iden:String = me.target.name;
			switch(iden){
				case 'closebtn':
				this.close();
				break;
				case 'surebtn':
				this.dispatchEvent(new Event(AbstractDialog.DIALOG_SURE));
				break;
			}
			
		}
		
		protected function layout(cdata:Object):void{
			throw new Error('you must override this function in child class');
		}
		
		public function show(cdata:Object):void{
			this.layout(cdata);
			this.toY = Math.ceil((644-this.h)/2);
			this.x = Math.ceil((779-this.w)/2);
			this.y = -this.h-30;
			this.state = 1;
			this.timer.start();
		}
		
		public function close():void{
			this.state = -1;
			this.timer.start();
		}
		
		public function getData():Object{
			throw new Error('you must override this function in child class');
		}
		
		protected function notifyTimer(te:TimerEvent):void{
			var cy:Number;
			if(this.state == 1){
				cy = (this.y+this.toY)/2;
				if((this.toY - this.y) > 10){
					this.y = cy;
				}else{
					this.y = this.toY;
					this.timer.stop();
				}
			}else if(this.state == -1){
				cy = (-this.h-30+this.y)/2;
				if((this.h+this.y) > 10){
					this.y = cy;
				}else{
					this.y = -this.h-30;
					this.timer.stop();
					this.dispatchEvent(new Event(AbstractDialog.DIALOG_CLOSE));
				}
			}
		}
		
	}
}